DUP

Section: MINTLIB LIBRARY FUNCTIONS (3)
Updated: 3 March 1993
Index Return to Main Contents
 

NAME

dup, dup2 - duplicate a file descriptor  

SYNOPSIS

#include <unistd.h>

int dup(int fd);

int dup2(int fd1, int fd2);
 

DESCRIPTION

dup duplicates an existing file descriptor. The argument fd is a small non-negative integer index in the per-process descriptor table. The new descriptor returned by the call is the lowest numbered descriptor that is not currently in use by the process. With dup2, fd2 specifies the desired value of the new descriptor. If descriptor fd2 is already in use, it is first deallocted as if it were closed by close. The new descriptor has the following in common with the original: - It refers to the same object that the old
  descriptor referred to. - It uses the same seek pointer as the old descriptor
  (that is, both file descriptors share one seek pointer). - It has the same access mode (read, write or read/write)
  as the old descriptor. Thus if fd2 and fd1 are duplicate references to an open file, read, write, and lseek calls all move a single seek pointer into the file, and append mode is shared between the references. If a separate seek pointer into the file is desired, a different object reference to the file must be obtained by issuing an additional open call.  

RETURN VALUES

dup and dup2 return a new descriptor on success. On failure, they return -1 and set errno to indicate the error.  

SEE ALSO

close(3), fcntl(3), lseek(3), open(3), pipe(3), read(3), write(3), Fdup(2), Fforce(2), Fcntl(2)


 

Index

NAME
SYNOPSIS
DESCRIPTION
RETURN VALUES
SEE ALSO

This document was created by man2html, using the manual pages.
Time: 11:14:43 GMT, June 22, 2025